//package com.example.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.example.bean.crm.skb;
//import com.example.bean.crm.skbmx;
//import com.example.service.crmService.crmService;
//import com.example.service.k3Service.k3Service;
//import com.example.utils.k3Api;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.Map;
//
///**
// * 收款订单
// * Created by tony on 2017/12/15.
// */
//@Component
//@RestController
//public class skControl {
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//    //定义k3Service
//    @Autowired
//    private k3Service k3Service;
//    //定义mesService
//    @Autowired
//    private crmService crmService;
//    public String token = "";
//
//    /**
//     * 获取Token
//     */
//    public boolean GetToken() {
//        k3Api k3Api = new k3Api();
//        String getTokenResult = k3Api.getToken();
//        //解析结果
//        Map result = (Map) JSON.parse(getTokenResult);
//        if (result.get("StatusCode").toString().equals("200")) {
//            Map data = (Map) JSON.parse(result.get("Data").toString());
//            //设置token值
//            token = data.get("Token").toString();
//            logger.info("Token取得成功，token值：" + token);
//            return true;
//        } else {
//            logger.info("Token取得失败，请检查网络");
//            return false;
//        }
//    }
//
//    /**
//     * 取得中间表收款信息，生成收款单
//     */
//    @RequestMapping("/Sk")
//    @Scheduled(cron = "0 0/5 * * * ?") //每5执行一次
//    public void insertSkScheduled() {
//        try {
//            //取得要处理的收款订单
//            ArrayList<skb> skbList = crmService.selectSkb();
//            //遍历收款订单
//            for (skb skb : skbList) {
//                //是否已存入erp
//                String erpFlg = skb.getERPskdaima();
//                //更新
//                if (erpFlg == null || erpFlg.isEmpty() || erpFlg.equals("")) {
//                    //新增
//                    insertSk(skb);
//                } else {
//
//                }
//            }
//        } catch (Exception e) {
//            logger.info(e.getMessage());
//        }
//
//
//    }
//
//    /**
//     * erp中插入收款
//     */
//    @RequestMapping("/insertSk")
//    public void insertSk(skb skb) {
//        if (GetToken()) {
//            //new
//            k3Api k3Api = new k3Api();
//            //操作url
//            String operation = "Bill1000005/Save";
//            try {
//                //收款订单id
//                String skbId = skb.getSkbID();
//                String hsxmlb = skb.getHsxmlb();
//                String hsxmdaima = skb.getHsxmdaima();
//                String hsxmname = skb.getHsxmname();
//                String djrq = skb.getDjrq();
//                String hsxmkhyh = skb.getHsxmkhyh();
//                String cwrq = skb.getCwrq();
//                //结算号
//                String jsh = "";
//                if (skb.getJsh() != null) {
//                    //新增
//                    jsh = skb.getJsh();
//                }
//                String hsxmyhzh = skb.getHsxmyhzh();
//                String hllx = skb.getHllx();
//                String zhaiyao = skb.getZhaiyao();
//                String bibie = skb.getBibie();
//                BigDecimal djje = skb.getDjje();
//                String zhanghao = skb.getZhanghao();
//                BigDecimal djjebwb = skb.getDjjebwb();
//                String sklx = skb.getSklx();
//                //param
//                String param = "{" +
//                        "  \"Data\": {" +
//                        "    \"Page1\": [" +
//                        "      {" +
//                        "        \"FMulCy\": \"\"," +
//                        "        \"FNumber\": \"\"," +
//                        "        \"FDate\": \"" + djrq + "\"," +
//                        "        \"FFincDate\": \"" + cwrq + "\"," +
//                        "        \"FItemClassID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"" + hsxmlb + "\"" +
//                        "        }," +
//                        "        \"FAdjustExchangeRate\": \"\"," +
//                        "        \"FCustomer\": {" +
//                        "          \"FNumber\": \"" + hsxmdaima + "\"," +
//                        "          \"FName\": \"" + hsxmname + "\"" +
//                        "        }," +
//                        "        \"FSettle\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FSettleNo\": \"" + jsh + "\"," +
//                        "        \"FClassTypeID\": \"\"," +
//                        "        \"FRPBank_Pay\": \"" + hsxmkhyh + "\"," +
//                        "        \"FBankAcct_Pay\": \"" + hsxmyhzh + "\"," +
//                        "        \"faccountid\": {" +
//                        "          \"FNumber\": \"1002.01.01\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FRPBank\": \"\"," +
//                        "        \"FBankAcct\": \"" + zhanghao + "\"," +
//                        "        \"FBillType\": \"\"," +
//                        "        \"FExchangeRateType\": {" +
//                        "          \"FNumber\": \"01\"," +
//                        "          \"FName\": \"公司汇率\"" +
//                        "        }," +
//                        "        \"FSettleNumber\": \"\"," +
//                        "        \"FCurrencyID\": {" +
//                        "          \"FNumber\": \"RMB\"," +
//                        "          \"FName\": \"人民币\"" +
//                        "        }," +
//                        "        \"FExchangeRate\": \"1\"," +
//                        "        \"FExplanation\": \"" + zhaiyao + "\"," +
//                        "        \"FAmountFor\": \"" + djje + "\"," +
//                        "        \"FSubSystemID\": \"\"," +
//                        "        \"FAmount\": \"" + djjebwb + "\"," +
//                        "        \"FSourceBillType\": \"\"," +
//                        "        \"FSourceBillNo\": \"\"," +
//                        "        \"FSettleAmount\": \"\"," +
//                        "        \"FContractNo\": \"\"," +
//                        "        \"FChecker\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FPreparer\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FDepartment\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FSettleDiscount\": \"\"," +
//                        "        \"FEmployee\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FTaskID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FReceiveCyID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FResourceID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FVoucherID\": \"\"," +
//                        "        \"FStatus\": \"\"," +
//                        "        \"FBudgetAmountFor\": \"\"," +
//                        "        \"FSettleCyID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FOrderID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FReceiveAmount\": \"\"," +
//                        "        \"FBillStatus\": \"\"," +
//                        "        \"FYear\": \"\"," +
//                        "        \"FPeriod\": \"\"," +
//                        "        \"FReceiveAmountFor\": \"\"," +
//                        "        \"FDiscountAmount\": \"\"," +
//                        "        \"FAdjustAmount\": \"\"," +
//                        "        \"FSettleAmountFor\": \"\"," +
//                        "        \"FDiscountAmountFor\": \"\"," +
//                        "        \"FOrderAmountFor\": \"\"," +
//                        "        \"FPrintCount\": \"\"," +
//                        "        \"FSettleDiscountFor\": \"\"," +
//                        "        \"FSource\": \"\"," +
//                        "        \"FSourceID\": \"\"," +
//                        "        \"FOrderNo\": \"\"," +
//                        "        \"FContractID\": \"\"," +
//                        "        \"FRP\": \"\"," +
//                        "        \"FRegion\": \"\"," +
//                        "        \"FTrade\": \"\"," +
//                        "        \"FPre\": \"\"," +
//                        "        \"FBankAcctName\": \"\"," +
//                        "        \"FObtainRateWay\": \"\"," +
//                        "        \"FConfirmAdvice\": \"\"," +
//                        "        \"FPaySettNo\": \"\"," +
//                        "        \"FConfirmor\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FConfirmDate\": \"\"," +
//                        "        \"FConfirmFlag\": \"\"" +
//                        "      }" +
//                        "    ]," +
//                        "    \"Page2\": [" +
//                        "      {" +
//                        "        \"FIndex2\": \"\"," +
//                        "        \"FSerial\": \"\"," +
//                        "        \"FExchangeCyID\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FExchangeAmountFor\": \"\"," +
//                        "        \"FExchangeExpenseFor\": \"\"," +
//                        "        \"FExchangeExpense\": \"\"," +
//                        "        \"FExchangerate2\": \"\"," +
//                        "        \"FExchangeAmount\": \"\"," +
//                        "        \"FSettleCyID2\": {" +
//                        "          \"FNumber\": \"\"," +
//                        "          \"FName\": \"\"" +
//                        "        }," +
//                        "        \"FSettleAmountFor2\": \"\"," +
//                        "        \"FSettleAmount2\": \"\"" +
//                        "      }" +
//                        "    ]," +
//                        "    \"Page3\": [" + danJuTi(skbId) +
//                        "    ]" +
//                        "  }" +
//                        "}";
//                //解析结果
//                Map result = (Map) JSON.parse(k3Api.callK3Api(operation, token, param));
//                logger.info(result.toString());
//                if (result.get("StatusCode").toString().equals("200")) {
//                    //已经成功添加
//                    skb.setClyf(2);
//                    //JSONArray jsonArray = JSONArray.parseArray(result.get("Data").toString());
//                    //Map information =jsonArray.getJSONObject(0);
//                    //返回erpid
//                    //skb.setERPxsddbh(information.get("FBillNo").toString());
//                    //返回信息
//                    //skb.setRtnInfo("添加成功！");
//
//                } else {
//                    //添加失败
//                    skb.setClyf(3);
//                    //返回信息
//                    //skb.setRtnInfo(result.get("Message").toString());
//                }
//
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                //异常
//                skb.setClyf(3);
//                //返回信息
//                //cgdd.setRtnInfo("异常操作，请联系管理员");
//            }
//            crmService.skbFlagRturn(skb);
//        }
//    }
//
//    /**
//     * 单据体拼接
//     */
//    public String danJuTi(String skbId) {
//        //单据体
//        String danJuTiString = "";
//        //取得销售订单明细
//        ArrayList<skbmx> skbmxList = crmService.selectSkbmx(skbId);
//        int count = 0;
//        for (skbmx skbmx : skbmxList) {
//            if (count == 0) {
//                danJuTiString = danJuTi2(skbmx);
//            } else {
//                danJuTiString = danJuTiString + "," + danJuTi2(skbmx);
//            }
//            count++;
//        }
//        return danJuTiString;
//    }
//
//    /**
//     * 单据体拼接2
//     */
//    public String danJuTi2(skbmx skbmx) {
//        //收款表明细ID
//        String skbID = skbmx.getSkbID();
//        //收款表ID
//        String skbmxID = skbmx.getSkbmxID();
//
//        //合同号
//        String hth = "";
//        if (skbmx.getHth() != null) {
//            //新增
//            hth = skbmx.getHth();
//        }
//        //订单号
//        String dddh = "";
//        if (skbmx.getDddh() != null) {
//            //新增
//            hth = skbmx.getDddh();
//        }
//        //结算数量
//        int jssl = skbmx.getJssl();
//        //结算金额
//        BigDecimal zjxh = skbmx.getJsssje();
//        //结算金额本位币
//        BigDecimal jjssjebwb = skbmx.getJjssjebwb();
//
//        String danJuTiString = " {" +
//                "        \"FBackAmountFor_Relative\": \"\"," +
//                "        \"FBackAmount_Relative\": \"\"," +
//                "        \"FIndex3\": \"\"," +
//                "        \"FLinkCheckQty\": \"\"," +
//                "        \"FLinkCheckAmountFor\": \"\"," +
//                "        \"FLinkCheckAmount\": \"\"," +
//                "        \"FClassID_SRC\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FBillNo_SRC\": \"\"," +
//                "        \"FEntryContractNo\": \"" + hth + "\"," +
//                "        \"FEntryOrderNo\": \"" + dddh + "\"," +
//                "        \"FReceiveCyName\": \"\"," +
//                "        \"FReceiveAmountFor_3\": \"\"," +
//                "        \"FReceiveAmount_3\": \"\"," +
//                "        \"FReceiveExchangeRate\": \"\"," +
//                "        \"FSettleCyName\": \"\"," +
//                "        \"FSettleQuantity\": \"" + jssl + "\"," +
//                "        \"FSettleAmountFor_3\": \"" + zjxh + "\"," +
//                "        \"FID_SRC\": \"\"," +
//                "        \"FReceiveCyID_3\": \"\"," +
//                "        \"FSettleCyID_3\": \"\"," +
//                "        \"FEntryID_SRC\": \"\"," +
//                "        \"FSettleAmount_3\": \"" + jjssjebwb + "\"," +
//                "        \"FDiscountFor_3\": \"\"," +
//                "        \"FDiscount\": \"\"," +
//                "        \"FExchangeExpenseFor3\": \"\"," +
//                "        \"FRemainAmountFor3\": \"\"," +
//                "        \"FRemainAmountFor_SRC\": \"\"," +
//                "        \"FRemainAmount3\": \"\"," +
//                "        \"FRemainAmount_SRC\": \"\"," +
//                "        \"FOrderEntryID\": \"\"," +
//                "        \"FContractEntryID\": \"\"," +
//                "        \"FExchangeExpense3\": \"\"," +
//                "        \"FOrderInterID\": \"\"," +
//                "        \"FSettleExchangeRate\": \"\"," +
//                "        \"FEntryAccountID\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FItemID\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FBaseProperty\": \"\"," +
//                "        \"FBaseProperty1\": \"\"," +
//                "        \"FAuxPropID\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FBase2\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FQuantity\": \"\"," +
//                "        \"FTaxPrice\": \"\"," +
//                "        \"FAmountFor_SRC\": \"\"," +
//                "        \"Famount_SRC\": \"\"," +
//                "        \"FCheckAmountFor\": \"\"," +
//                "        \"FCheckAmount\": \"\"," +
//                "        \"FAmountFor_Entry\": \"\"," +
//                "        \"FRemainAmountForEntry\": \"\"," +
//                "        \"Famount_Entry\": \"\"," +
//                "        \"FRemainAmountEntry\": \"\"," +
//                "        \"FRemainQty\": \"\"," +
//                "        \"FFeeObjID\": {" +
//                "          \"FNumber\": \"\"," +
//                "          \"FName\": \"\"" +
//                "        }," +
//                "        \"FFeeObjName\": \"\"," +
//                "        \"FConfirmAdviceEntry\": \"\"" +
//                "      }";
//        return danJuTiString;
//
//    }
//}
